<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\sys\SysSetting;
use yii\web\JsExpression;
/* @var $this yii\web\View */
/* @var $model common\models\sys\SysSetting */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <div class="adv-table editable-table ">
                <div class="sys-setting-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ]
                    ]); ?>
                        <div class="form-group field-syssetting-keyname required has-success">
                            <div class="col-xs-3 col-sm-2 text-right">
                                <label class="control-label" for="syssetting-keyname">设置项名称</label>
                            </div>
                            <div class="col-xs-9 col-sm-7"><input type="text" id="syssetting-keyname" class="form-control" name="keyname" readonly="readonly"   value="<?= $model->keyname?>" maxlength="1000" aria-required="true" aria-invalid="false">
                            </div>
                            <div class="col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0">
                                <div class="help-block">
                                </div>
                            </div>
                        </div>

                        <?php if($model->viewtype == SysSetting::TYPE_INPUT && $model->keyvalue){ ?>
                            <?php foreach($model->keyvalue as $key => $row){ ?>
                                <div class="form-group field-syssetting-keyvalue required has-success">
                                    <div class="col-xs-3 col-sm-2 text-right">
                                        <label class="control-label" for="syssetting-keyvalue">设置项值</label>
                                    </div>
                                    <div class="col-xs-9 col-sm-7"><input type="text" id="syssetting-keyvalue" class="form-control" name="keyvalue[]" value="<?= $row ?>" maxlength="1000" aria-required="true" aria-invalid="false">
                                    </div>
                                    <div class="col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0">
                                        <div class="help-block">
                                            <?= $model->help ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }?>
                        <?php } ?>


                    <?php if($model->viewtype == SysSetting::TYPE_INPUT) { ?>
                        <div class="form-group field-syssetting-keyvalue required has-success" id="copy" style="display:none">
                            <div class="col-xs-3 col-sm-2 text-right">
                                <label class="control-label" for="syssetting-keyvalue">设置项值</label>
                            </div>
                            <div class="col-xs-9 col-sm-7"><input type="text" id="syssetting-keyvalue" class="form-control" name="keyvalue[]" value="" maxlength="1000" aria-required="true" aria-invalid="false">
                            </div>
                            <div class="col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0">
                                <div class="help-block">
                                </div>
                            </div>
                        </div>
                    <?php } else if($model->viewtype == SysSetting::TYPE_FILE) { ?>
                        <?= $form->field($model, 'keyvalue')->widget(\kartik\file\FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->keyvalue,true,'keyvalue'),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'keyvalue',SysSetting::tableName(),$model->identify,'identify'),
                                'pluginLoading' => true,
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                            ],
                        ]); ?>
                    <?php  } else if($model->viewtype == SysSetting::TYPE_COLOR) { ?>
                        <?= $form->field($model, 'keyvalue')->widget( \kartik\color\ColorInput::classname(), [
                            'options' => ['placeholder' => '点击左边选择颜色']
                        ]); ?>
                    <?php } ?>

                    <?php if(Yii::$app->user->id == '1') { ?>
                    <?= $form->field($model, 'canuser')->widget(\kartik\select2\Select2::className(), [
                        'options' => ['placeholder' => '请输入设置此项用户的名称 ...'],
                        'initValueText'=>$model->canuser?\common\models\AdminModel::findOne($model->canuser)->username:'',
                        'pluginOptions' => [
                            'allowClear' => true,
                            'minimumInputLength' => 1,//重要
                            'language' => [
                                'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                            ],
                            'ajax' => [
                                'url' => '/business/search/search-admin',
                                'dataType' => 'json',
                                'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                'cache' => true
                            ],
                            'width' => '400px',
                            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                            'templateResult' => new JsExpression('function(res) { return res.text; }'),
                            'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                        ],
                    ]);
                    ?>
                    <?php  } ?>


                    <input type="hidden" name="identify" value="<?=$model->identify ?>">

                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

                        <?php if($model->ismuti == \common\models\sys\SysSetting::MUTI) { ?>
                            <?= Html::label('添加项','',['class'=>'btn btn-primary','id'=>'ccopy'])?>
                        <?php } ?>
                    </div>
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>


<script>

    <?php $this->beginBlock('js_end') ?>
    $('#ccopy').click(function () {
        var copy = $('#copy');
        $('#copy').after(copy.clone()).attr('id','').css('display','block');
    });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>